package com.summer.framework.core.handler;

import com.summer.framework.base.exception.SummerException;
import com.summer.framework.base.exception.SummerRuntimeException;
import com.summer.framework.base.result.Result;
import com.summer.framework.base.result.ResultUtil;
import com.summer.framework.core.i18n.I18nUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

/**
 * Description: 异常信息统一处理
 * @auther Ice Yang
 * Date 2025/7/2 13:41
 */
@RestControllerAdvice
@Order
public class SummerExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(SummerExceptionHandler.class);

    private final static int ERROR_CODE = 550;

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(SummerException.class)
    public Result<String> handleDraftException(SummerException e){
        String message = I18nUtils.getMessage(e.getMessageKey(),e.getMessage());
        return ResultUtil.error(e.getCode(), message);
    }

    @ExceptionHandler(SummerRuntimeException.class)
    public Result<String> handleDraftRuntimeException(SummerRuntimeException e){
        String message = I18nUtils.getMessage(e.getMessageKey(),e.getMessage());
        return ResultUtil.error(e.getCode(),message);
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public Result<String> handleDuplicateKeyException(DuplicateKeyException e){
        logger.error(e.getMessage(), e);
        String message = I18nUtils.getMessage("data.has.exist","数据已经存在");
        return ResultUtil.error(ERROR_CODE,message);
    }

    @ExceptionHandler(BindException.class)
    public Result<String> handleBindException(BindException e){
        logger.error(e.getMessage(), e);
        String message = I18nUtils.getMessage("param.has.error","参数有异常");
        return ResultUtil.error(ERROR_CODE,message);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result<String> handleMethodException(HttpRequestMethodNotSupportedException e){
        logger.error("This request type is not supported", e);
        String message = I18nUtils.getMessage("method.not.allowed","不支持此请求方法");
        return ResultUtil.error(ERROR_CODE,message);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result<String> handleNullPointerException(HttpMessageNotReadableException e){
        logger.error(e.getMessage(), e);
        String message = I18nUtils.getMessage("param.is.empty","请求参数为空");
        return ResultUtil.error(ERROR_CODE,message);
    }

    @ExceptionHandler(Exception.class)
    public Result<String> handleException(Exception e){
        logger.error(e.getMessage(), e);
        if(null != e.getCause() && (e.getCause().getClass() == SummerException.class || e.getCause().getClass() == SummerRuntimeException.class)){
            return ResultUtil.error(ERROR_CODE,e.getCause().getMessage());
        }
        String message = I18nUtils.getMessage("system.has.error","系统异常，联系管理员");
        return ResultUtil.error(ERROR_CODE,message);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> handleValidException(MethodArgumentNotValidException e){
        logger.error(e.getLocalizedMessage(), e);
        String message = e.getMessage();
        String[] msgs = message.split("default message");
        if(msgs.length < 2){
            return ResultUtil.error(ERROR_CODE,"参数异常");
        }
        return ResultUtil.error(ERROR_CODE,msgs[msgs.length - 1].replace("[","").replace("]",""));
    }
}
